package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.consts.EnvEnum;
import com.harmontronics.hanma.paas.paas.utils.RequestUtils;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmReleaseRecordAppService;
import com.harmontronics.hanma.paas.paas.dto.releasesetup.output.HmReleaseRecordResult;
import com.harmontronics.hanma.paas.paas.dto.releasesetup.input.HmReleaseRecordCreateInput;
import com.harmontronics.hanma.paas.paas.domain.releasesetup.entity.HmReleaseRecord;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 应用发布记录表
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-14 15:55:21
 */
@RestController
@RequestMapping("/api/hm-release-record")
@Api(tags = {"应用发布记录表"})
public class HmReleaseRecordController {
    @Autowired
    private HmReleaseRecordAppService hmReleaseRecordappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmReleaseRecordResult>> list() {
        List<HmReleaseRecord> hmReleaseRecordList = hmReleaseRecordappService.getList();

        return ResponseEntity.ok(Convert.toList(HmReleaseRecordResult.class, hmReleaseRecordList));
    }

    /**
    * 分页列表
    */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmReleaseRecordResult>> list(
            @ApiParam(value = "环境") @RequestParam(required = false)EnvEnum setupType,
            @ApiParam(value = "页码", defaultValue = "1")@RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize) {
        String appUnique = RequestUtils.getAppUnique();
        PageBase<HmReleaseRecord> hmReleaseRecordPageBase = hmReleaseRecordappService.getList(setupType,pageNo, pageSize,appUnique);
        PageBase<HmReleaseRecordResult> resultPageBase = PageBase.copy(hmReleaseRecordPageBase,HmReleaseRecordResult.class);
        return ResponseEntity.ok(resultPageBase);
    }

    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmReleaseRecordCreateInput hmReleaseRecordInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmReleaseRecord hmReleaseRecord =BeanUtil.toBean(hmReleaseRecordInput,HmReleaseRecord. class);
            hmReleaseRecordappService.save(hmReleaseRecord);

        return ResponseEntity.ok();
    }


}
